<!--
 * @Date: 2024-04-16 13:42:49
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-08-06 09:51:43
 * @Description: 授权中转页面
-->
<template>
  <div class="oauth-wrapper">
    <div class="oauth-info-wrapper">
      <div class="oauth-info-card">
        <div class="oauth-info-title">
          <span>系统提示</span>
        </div>
        <div class="oauth-info-body">
          <span>{{ oauthInfo.errorMsg }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router';

import { formatAxis } from '/@/utils/formatTime';
import { Session, NextLoading } from '@/utils'

import { useSystemStore } from '@/stores/modules/store-system';
import { initBackEndControlRoutes } from '/@/router/backEnd';

const router = useRouter();
const { query: routerQuery } = useRoute();

/**
 * 授权信息
 */
const oauthInfo = reactive({
  params: {
    accessToken: ''
  },
  loading: false,
  errorMsg: '系统未授权，请联系管理员！'
})

const loadOauth = async () => {
  NextLoading.start();
  oauthInfo.loading = true
  if (routerQuery.hasOwnProperty('token')) {
    const { token } = routerQuery
    if (token) {
      const { flag, message } = await useSystemStore().signInSSO({ accessToken: token });
      if (flag) {
        // 登录成功
        // 添加完动态路由，再进行 router 跳转，否则可能报错 No match found for location with path "/"
        const isNoPower = await initBackEndControlRoutes();
        signInSuccess(isNoPower)
      } else {
        // 登录失败
        oauthInfo.errorMsg = message
      }
    } else {
      oauthInfo.errorMsg = 'token内容不能为空, 请确认！'
    }
  } else {
    oauthInfo.errorMsg = 'token参数不存在, 请确认！'
  }
  NextLoading.done();
}

// 时间获取
const currentTime = computed(() => {
  return formatAxis(new Date());
});

// 登录成功后的跳转
const signInSuccess = (isNoPower) => {
  if (isNoPower) {
    useSystemStore().clearAllCache()
    Session.clear();
  } else {
    // 初始化登录成功时间问候语
    let currentTimeInfo = currentTime.value;
    // 登录成功，跳到转首页
    router.push('/');
    // 登录成功提示
    const signInText = '欢迎回来！';
    ElMessage.success(`${currentTimeInfo}，${signInText}`);
  }
  oauthInfo.loading = false
};

loadOauth()

</script>

<style lang="scss" scoped>
.oauth-info {
  &-wrapper {
    width: 400px;
    margin: 0 auto;
    padding-top: 100px;
  }
  &-card {
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(12,12,12,0.09);
  }
  &-title {
    padding: 0 10px;
    color: #fff;
    border-radius: 4px 4px 0 0;
    background-color: #9d9790;
    height: 40px;
    line-height: 40px;
  }
  &-body {
    padding: 10px;
    border: 4px solid #9d9790;
    border-top: none;
    height: 150px;
    border-radius: 0 0 4px 4px;
  }
  
}
</style>